﻿using System;
using System.Collections.Generic;
using System.Linq;
using Accountant.DataModel;
using Accountant.Utils;

namespace Accountant.Primitives
{
  public sealed class ExchangeTransaction : GroupTransaction
  {
    public Account Debit
    {
      get { return Transactions[0].Debit; }
      set { Transactions[0].Debit = value; }
    }
    public Account Credit
    {
      get { return Transactions[1].Credit; }
      set { Transactions[1].Credit = value; }
    }

    public decimal DebitAmount
    {
      get { return Transactions[0].Amount; }
      set { Transactions[0].Amount = value; }
    }
    public decimal CreditAmount
    {
      get { return Transactions[1].Amount; }
      set { Transactions[1].Amount = value; }
    }

    public Currency DebitCurrency
    {
      get { return Transactions[0].Currency; }
      set { Transactions[0].Currency = value; }
    }
    public Currency CreditCurrency
    {
      get { return Transactions[1].Currency; }
      set { Transactions[1].Currency = value; }
    }

    public string DebitMoneyString { get { return DebitMoney.ToString(); } }
    public string CreditMoneyString { get { return CreditMoney.ToString(); } }
    public Money DebitMoney { get { return new Money(DebitCurrency, DebitAmount); } }
    public Money CreditMoney { get { return new Money(CreditCurrency, CreditAmount); } }

    public ExchangeTransaction(List<Transaction> transactions) 
      : base(transactions)
    {
      if (transactions.Count != 2) throw new ArgumentException("Exchange transaction consist of 2 transactions");
      if (!transactions.All(t => t.IsExchange())) throw new ArgumentException("Group exchange transaction must consist of 2 exchange transactions");
    }
  }
}